#include "model/sphere.h"
#include <cmath>

#define PI 3.141592654
#define UHOL_KROK ((2*PI/360.0)*10.0)

Sphere::Sphere(const Point &point, double r) {
    std::vector<Vertex> vertexs;

    #define POLUDNIKOV 36
    #define ROVNOBEZIEK 18
    int p2 = POLUDNIKOV / 2;
    int r2 = ROVNOBEZIEK / 2;
    for(int y = -r2; y < r2; ++y) {
        float cy = cos(y*UHOL_KROK);
        float cy1 = cos((y+1)*UHOL_KROK);
        float sy = sin(y*UHOL_KROK);
        float sy1 = sin((y+1)*UHOL_KROK);

        for(int i = -p2; i < p2; ++i) {
            float ci = cos(i*UHOL_KROK);
            float si = sin(i*UHOL_KROK);
            vertexs.push_back(Vertex(point.x+r * ci*cy, point.y+r*sy, point.z+r * si*cy));
            vertexs.push_back(Vertex(point.x+r * ci*cy1, point.y+r*sy1, point.z+r * si*cy1));
        }
    }

    for ( unsigned int i = 0; i < (vertexs.size()-2); i +=2 ) {
        this->add(std::vector<Vertex>({
                                          vertexs[i], vertexs[i+1], vertexs[i+2]
                                      }));
        if (i+1 <= vertexs.size()-2)
        this->add(std::vector<Vertex>({
                                          vertexs[i+1], vertexs[i+3], vertexs[i+2]
                                      }));

    }
}

